﻿/****************************** Module Header ******************************\
 * Module Name:  mxDataTable.cs
 * Module Version:  1.0.0 (2012-10-27)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension collection instance's method.  It supplies 
 * useful extension methods, such as ToString(), FetchString() and so on.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;

namespace UCL
{
    public static class mxDateTime
    {
        public static int GetWeekOfYear(this DateTime v)
        {
            int w = v.DayOfYear / 7;
            if (v.DayOfYear % 7 > 0) w += 1;
            return w;
        }

        /// <summary>
        /// 倒计时(天时分秒) 2013-6-17 by sutroon
        /// </summary>
        /// <param name="v"></param>
        /// <param name="dtNow"></param>
        /// <returns></returns>
        public static string CountDown(this DateTime v, DateTime dtNow)
        {
            TimeSpan ts = v - DateTime.Now; //时间差
            return string.Format("{0}天{1}时{2}分{3}秒", ts.Days.ToString().PadLeft(2, '0'), ts.Hours.ToString().PadLeft(2, '0'), ts.Minutes.ToString().PadLeft(2, '0'), ts.Seconds.ToString().PadLeft(2, '0'));
        }

        /// <summary>
        /// 判断是否在指定类型的日期范围内 2013-6-20 by sutroon
        /// <para>类型：week、month、quarter、year</para>
        /// </summary>
        /// <param name="v"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool IsInDayRange(this DateTime v, DateTime dt, string type = "week")
        {
            DateTime first, last;
            switch (type.ToLower())
            {
                case "week":
                    int dayOfWeek = Convert.ToInt32(dt.DayOfWeek.ToString("d"));
                    first = dt.AddDays(1 - ((dayOfWeek == 0) ? 7 : dayOfWeek));         //本周周一
                    last = first.AddDays(6);                                            //本周周日
                    break;
                case "month":
                    first = dt.AddDays(1 - dt.Day);                                     //本月月初
                    last = first.AddMonths(1).AddDays(-1);                              //本月月末
                    break;
                case "quarter":
                    first = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day);   //本季度初
                    last = first.AddMonths(3).AddDays(-1);                              //本季度末
                    break;
                case "year":
                    first = new DateTime(dt.Year, 1, 1);                                //本年年初
                    last = new DateTime(dt.Year, 12, 31);                               //本年年末
                    break;
                default:
                    return false;
            }
            return v >= first && v <= last;
        }

        /// <summary>
        /// 获取指定类型的第一天 2013-6-20 by sutroon
        /// <para>类型：week、month、quarter、year</para>
        /// </summary>
        /// <param name="v"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DateTime GetFirstDay(this DateTime v, string type = "week")
        {
            switch (type.ToLower())
            {
                case "week":
                    int dayOfWeek = Convert.ToInt32(v.DayOfWeek.ToString("d"));
                    return v.AddDays(1 - ((dayOfWeek == 0) ? 7 : dayOfWeek));
                case "month":
                    return v.AddDays(1 - v.Day);
                case "quarter":
                    return v.AddMonths(0 - (v.Month - 1) % 3).AddDays(1 - v.Day);
                case "year":
                    return new DateTime(v.Year, 1, 1);
            }
            return DateTime.Today;
        }

        /// <summary>
        /// 获取指定类型的最后一天 2013-6-20 by sutroon
        /// <para>类型：week、month、quarter、year</para>
        /// </summary>
        /// <param name="v"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DateTime GetLastDate(this DateTime v, string type = "week")
        {
            switch (type.ToLower())
            {
                case "week":
                    return v.GetFirstDay(type).AddDays(6);
                case "month":
                    return v.GetFirstDay(type).AddMonths(1).AddDays(-1);
                case "quarter":
                    return v.GetFirstDay(type).AddMonths(3).AddDays(-1);
                case "year":
                    return v.GetFirstDay(type).AddYears(1).AddDays(-1);
                default:
                    return DateTime.Today;
            }
        }

    }
}
